package cn.schoolwow.workflow.module.instance.query.service;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.instance.InstanceContextDataRequest;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowTask;
import com.alibaba.fastjson.JSONObject;

public class GetInstanceContextDataFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        DAO dao = flowContext.checkInstanceData(DAO.class);
        InstanceContextDataRequest contextDataRequest = flowContext.checkInstanceData(InstanceContextDataRequest.class);

        Long workFlowInstanceId = contextDataRequest.workFlowInstanceId;
        if(null==workFlowInstanceId){
            dao.addRecordString("根据工作流任务id查询实例id");
            workFlowInstanceId = dao.query(WorkFlowTask.class)
                    .addQuery("id", contextDataRequest.workFlowTaskId)
                    .addColumn("instanceId")
                    .execute()
                    .getSingleColumn(Long.class);
        }

        dao.addRecordString("根据实例id查询实例上下文数据");
        JSONObject contextData = dao.query(WorkFlowInstance.class)
                .addColumn("contextData")
                .addQuery("id", workFlowInstanceId)
                .execute()
                .getSingleColumn(JSONObject.class);
        flowContext.putTemporaryData("contextData", contextData);
    }

    @Override
    public String name() {
        return "获取工作流实例上下文数据服务流程";
    }
}
